package com.deductive.web.disposition.sw;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.core.page.TableDataInfo;
import com.deductive.common.utils.DateUtils;
import com.deductive.domain.layer.Models3d;
import com.deductive.domain.sw.DispositionSwJt;
import com.deductive.service.layer.Models3dService;
import com.deductive.service.sw.DispositionSwJtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 舰艇类.
 *
 * @author yexiaolong
 * @version 1.0
 * @since 2025/8/29
 */
@RestController
@RequestMapping("/postDeliveryTool")
@Api(tags = "后送工具管理")
public class WarDeliveryToolController extends BaseController {

    @Resource
    private DispositionSwJtService jtService;

    @Resource
    private Models3dService models3dService;

    @GetMapping("/list")
    @ApiOperation("后送工具查询列表")
    public TableDataInfo list(DispositionSwJt hsll) {
        startPage();
        LambdaQueryWrapper<DispositionSwJt> eq = new LambdaQueryWrapper<>();
        eq.like(StringUtils.isNotBlank(hsll.getName()), DispositionSwJt::getName, hsll.getName());
        List<DispositionSwJt> list = jtService.list(eq);
        for (DispositionSwJt dispositionSwJt : list) {
            Long model3dId = dispositionSwJt.getModel3dId();
            if (model3dId != null) {
                Models3d models3d = models3dService.getById(model3dId);
                dispositionSwJt.setModelName(models3d.getName());
                String url = models3d.getUrl();
                dispositionSwJt.setModelUrl(url);
            }
        }
        return getDataTable(list);
    }

    @PutMapping("/update")
    @ApiOperation("后送工具修改")
    public AjaxResult update(@RequestBody DispositionSwJt hsll) {
        try {
            hsll.setUpdateTime(DateUtils.getNowDate());
            return AjaxResult.success(jtService.updateById(hsll));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage(), false);
        }
    }

    @PostMapping("/insert")
    @ApiOperation("后送工具新增")
    public AjaxResult insert(@RequestBody DispositionSwJt hsll) {
        try {
            hsll.setCreateTime(DateUtils.getNowDate());
            return AjaxResult.success(jtService.save(hsll));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage(), false);
        }
    }

    @DeleteMapping
    @ApiOperation("后送工具删除")
    public AjaxResult delete(@RequestParam("idList") List<Integer> idList) {
        return AjaxResult.success(jtService.removeByIds(idList));
    }

}
